<template>
	<view class="menus">
		<nav-bar></nav-bar>
		<view class="title">{{lang.title}}</view>
		<view class="subtitle">{{lang.subtitle}}</view>
		<view class="list">
			<view class="item" v-for="(item, index) in lang.list" :key="index" @click="openPage(item)">
				<image :src="'https://shop.outletds.com/img/' + item.icon" mode="aspectFit"></image>
				<text>{{item.name}}</text>
			</view>
		</view>
		<view class="btn" @click="logout">退出登录</view>
	</view>
</template>

<script>
	import {
		storeInfoApi,
		accountInfoApi,
		writeOffCouponApi,
		verifiedOrderApi
	} from "@/api/store.js"
	import {shareLevel} from "@/utils/api"
	import colorGradient from "uview-ui/libs/function/colorGradient"
	export default {
		data() {
			return {
				isAdmin: true,
				adminTitle: {
					title: '审核管理',
					subtitle: '奥特莱斯门店管理',
					list: [{
							icon: '/daishenhe-3.png',
							name: '待审核',
							link: '/pages/store/order-record/order-record',
							query: {
								status: 1
							}
						},
						{
							icon: 'yishenhe-2.png',
							name: '审核管理',
							link: '/pages/store/order-record/order-record'
						},
						{
							icon: 'fuwujilu 2.png',
							name: '核销记录',
							link: '/pages/store/write-off-record/write-off-record'
						},
					],
				},
				staffTitle: {
					title: '店铺',
					subtitle: '奥特莱斯门店管理',
					list: [{
							icon: 'huiyuanqia-copy.png',
							name: '订单录入',
							link: '/pages/store/reception/reception'
						},
						{
							icon: 'fuwujilu.png',
							name: '录单记录',
							link: '/pages/store/order-record/order-record'
						},
						{
							icon: 'saoma.png',
							name: '线下核销',
							link: '1'
						},
						{
							icon: 'fuwujilu 2.png',
							name: '核销记录',
							link: '/pages/store/write-off-record/write-off-record'
						},
					],
				},
				type: 2,
				info: {}
			}
		},
		computed: {
			lang() {
				return this.type == 1 ? this.staffTitle : this.adminTitle
			}
		},
		onLoad() {
			this.getData()
		},
		methods: {
			openPage(item) {
				if (item) {
					if (item.link.match(/^\//)) {
						console.log('into');
						let link = item.link + `?type=${this.type}`
						if (item.query) {
							for (var key in item.query) {
								link += `&${key}=${item.query[key]}`
							}
						}
						this.$Router.push(link)
					} else {
						// 特殊行为
						switch (item.link) {
							// 核销
							case "1":
								this.writeOff()
						}
					}

				}
			},
			async getData() {
				let {
					data
				} = await storeInfoApi()
				this.info = data
				this.staffTitle.title = this.info.title
				let res = await accountInfoApi()
				this.type = res.data.type
			},
			logout() {
				uni.removeStorageSync('store_token')
				this.$Router.replace('/pages/store/login/login')
			},
			writeOff() {
				uni.scanCode({
					success: (res) => {
						let data = null
						try {
							data = JSON.parse(res.result)
							
						} catch (err) {
							this.$utils.toast('二维码错误')
						}
						if( (!data) || !data.type)return this.$utils.toast('二维码错误')
						switch(data.type){
							case 2: 
								this.verifiedOrder(data.data)
								break;
							default: 
								this.$utils.toast('二维码错误')
						}
					},
					fail: (err) => {
						this.$utils.toast('二维码错误')
					}
				})
			},
			userCoupon(){
				writeOffCouponApi(data).then((resp) => {
					this.$utils.toast(resp.msg)
				})
			},
			// 核销订单
			async verifiedOrder(data){
				await verifiedOrderApi(data)
				this.$utils.success('订单核销成功')
			},
			// 绑定上下级
			bindUser(id){
				shareLevel({
					id: id,
				}).then((resp) => {
					console.log("用户上下级绑定", resp);
				});
			}
		}
	}
</script>

<style scoped lang="scss">
	@import 'index.scss';
</style>
